﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Diagnostics;

namespace WpfDecorator
{
    public class ReflectorAdorner : Adorner
    {
        Rectangle rec;
        VisualCollection VisualChildren;

        public ReflectorAdorner(UIElement adnoredElement)
            : base(adnoredElement)
        {
            rec = new Rectangle();
            VisualChildren = new VisualCollection(this);
            AddReflection(adnoredElement);
        }

        void AddReflection(UIElement element)
        {
            //产生倒影的Rectangle
            rec.RenderTransformOrigin = new Point(.5, .5);
            rec.Margin = new Thickness(0, 1, 0, 0);

            VisualBrush brush = new VisualBrush(element) { Stretch = Stretch.Fill, AlignmentY = AlignmentY.Bottom };
            rec.Fill = brush;

            LinearGradientBrush linerBrush = new LinearGradientBrush() { StartPoint = new Point(.5, 1), EndPoint = new Point(0.5, 0) };
            linerBrush.GradientStops.Add(new GradientStop() { Color = Colors.Black, Offset = 0.124 });
            linerBrush.GradientStops.Add(new GradientStop() { Color = Color.FromArgb(0, 255, 255, 255), Offset = 1 });
            rec.OpacityMask = linerBrush;

            rec.RenderTransform = new ScaleTransform() { ScaleX = 1, ScaleY = -1 };
         
            VisualChildren.Add(rec);
        }

        public void HiddenReflector()
        {
            rec.Visibility = rec.Visibility == Visibility.Hidden ? Visibility.Visible : Visibility.Hidden;
        }


        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return rec;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            rec.Width = this.DesiredSize.Width;
            rec.Height = this.DesiredSize.Height;
            rec.Arrange(new Rect(0, this.DesiredSize.Height, this.DesiredSize.Width, this.DesiredSize.Height));
            return base.ArrangeOverride(finalSize);
        }
    }
}
